/**
 * 
 */
package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * @author Rodrigo
 *
 */
public class AcceptClients extends Thread{

	private Server server;
	private ServerSocket sSocket;
	private volatile boolean stop = false;
	
	public AcceptClients(Server server, ServerSocket sSocket) {
				
		this.sSocket = sSocket;
		this.server = server;
		
	}
	 
	public void requestStop() {
		    stop = true;
	}	
	
	
	@Override
	public void run() {


		// LOOP UNTIL RECEIVE A STOP REQUEST, ACCEPTING CLIENTS 
		while(!stop){ 

			try {

				// Wait for new client
				Socket nextClient = sSocket.accept(); 

				Player p = new Player(server, nextClient);
				p.start();

				server.getData().getPlayers().add(p);


			} 
			catch(SocketTimeoutException e){
				// timeout

			}catch (IOException ioexc){ 
				System.err.println ("[AcceptClients] I/O error - " + ioexc); 

			}catch (Exception ex){ 
				System.err.println ("[AcceptClients] Exception - " + ex); 
			}

		}

	}

	

	
}
